package system;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import bean.UserBean;
import dao.DAO;
import dao.DBFacade;
import dao.UserDAO;
import exceptions.ExistingEmailException;
import exceptions.ExistingLoginException;
import exceptions.InvalidAttributeException;
import exceptions.InvalidBlogException;
import exceptions.InvalidDateException;
import exceptions.InvalidEmailException;
import exceptions.InvalidGenderException;
import exceptions.InvalidLoginException;
import exceptions.InvalidPasswordException;

/**
 * Class responsible for the operations of blog
 * @author Anne Lorayne, Laura Dutra, Otavio Lima
 * 
 */
public class User {

	private static DAO<UserBean> userDAO = UserDAO.getInstance(); 
	private UserBean user;
	private List<Blog> blogList = new ArrayList<Blog>();
	private List<Announcement> announcementList;

	
	//User Operations

	/**
	 * 
	 * @param login
	 * @param password
	 * @param name
	 * @param email
	 * @param gender
	 * @param dateOfBirth
	 * @param address
	 * @param interests
	 * @param whoIAm
	 * @param movies
	 * @param musics
	 * @param books
	 * @throws Exception
	 */
	public User(String login, String password, String name, String email, 
			String gender, String dateOfBirthString, String address, String interests, 
			String whoIAm, String movies, String musics, String books) throws Exception {
		if (login == null || login.equals(""))
			throw new InvalidLoginException();
		if (password == null || password.equals(""))
			throw new InvalidPasswordException();
		if (email == null || email.equals(""))
			throw new InvalidEmailException();
		if (gender == null || !(gender.equals("Feminino") || gender.equals("Masculino") || gender.equals("Não informado")))
			throw new InvalidGenderException();
		Calendar dateOfBirth = parseStringToDate(dateOfBirthString);
		if (dateOfBirth == null)
			throw new InvalidDateException();


		Iterator<UserBean> users = userDAO.list().iterator();
		long id = DBFacade.getInstance().getLastUserId();
		String ID = "u" + id;
		while(users.hasNext()){
			UserBean anotherUser = users.next();
			if (anotherUser.getLogin().equals(login))
				throw new ExistingLoginException();
			else if (anotherUser.getEmail().equals(email))
				throw new ExistingEmailException();
			else if (anotherUser.getId().equals("0"))
				; // TODO add logica se ja tiver id!
			else
				;
		}
		
		
		user = new UserBean(login, password, name, email, gender, dateOfBirth, address, interests, whoIAm, movies, musics, books, ID);
		userDAO.save(user);
		
		blogList = Blog.getAllBlogsOfUser(ID);
		//TODO Persistence?
		announcementList = new ArrayList<Announcement>();
	}

	/**
	 * Create a user instance given an UserBean
	 * @param user - User's to be created
	 */
	public User(UserBean user) {
		this.user = user;
		try {
			blogList = Blog.getAllBlogsOfUser(user.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		announcementList = new ArrayList<Announcement>();
		
	}

	/**
	 * Returns user's instance
	 * @return UserBean
	 */
	public UserBean getInstanceUser() {
		if (user == null)
			user = new UserBean();
		return user;
	}

	/**
	 * Return blogs list's instance
	 * @return blogs list
	 */
	public List<Blog> getInstanceBlogList() {
		if (blogList == null)
			blogList = new ArrayList<Blog>();
		return blogList;
	}

	/**
	 * Do changes on user's profile
	 * @param attribute - Attribute to be updated
	 * @param newValue - New value for attribute
	 * @return - true if could update the user, false otherwise
	 * @throws Exception - Throw exception if given attribute is not valid 
	 */
	public boolean updateUser(String attribute, String newValue) throws Exception {
		if (attribute == null)
			throw new InvalidAttributeException();
		if(attribute.equals("senha") && (newValue == null || newValue.equals("") ))
			throw new InvalidPasswordException(); 
		if (attribute.equals("sexo"))
			if (newValue == null || !(newValue.equals("Feminino") || newValue.equals("Masculino") || newValue.equals("Não informado")))
				throw new InvalidGenderException();
		if(attribute.equals("email") && (newValue == null || newValue.equals("") ))
			throw new InvalidEmailException();
		if(attribute.equals("login") && (newValue == null || newValue.equals("") ))
			throw new InvalidLoginException();

		Iterator<UserBean> users = userDAO.list().iterator();

		while(users.hasNext()){
			UserBean anotherUser = users.next();							
			if (anotherUser.getEmail().equals(newValue) && !anotherUser.getLogin().equals(this.user.getLogin()))
				throw new ExistingEmailException();
			else if (anotherUser.getLogin().equals(newValue) && !anotherUser.getLogin().equals(this.user.getLogin()))
				throw new ExistingLoginException();
			else
				;
		}

		users = userDAO.list().iterator();
		Map<String, String> pt_to_en = new HashMap<String,String>();

		{
			pt_to_en.put("nome_exibicao", "Name");
			pt_to_en.put("login", "Login");
			pt_to_en.put("email", "Email");
			pt_to_en.put("sexo", "Gender");
			pt_to_en.put("endereco", "Address");
			pt_to_en.put("interesses", "Interests");
			pt_to_en.put("quem_sou_eu", "WhoIAm");
			pt_to_en.put("filmes", "Movies");
			pt_to_en.put("livros", "Books");
			pt_to_en.put("musicas", "Musics");
			pt_to_en.put("senha", "Password");
		}


		Object[] args = {newValue};


		while(users.hasNext()){
			UserBean anotherUser = users.next();							
			if (anotherUser.getLogin().equals(this.user.getLogin())){
				if(attribute.equals("dataNasc")){
					Calendar dateOfBirth = parseStringToDate(newValue);
					if (dateOfBirth == null)
						throw new InvalidDateException();
					anotherUser.setDateOfBirth(dateOfBirth);

				} else {
					try {
						Class<? extends UserBean> userBeanClass = anotherUser.getClass();
						Method thisMethod = userBeanClass.getMethod("set" + pt_to_en.get(attribute), new Class[] {String.class});
						thisMethod.invoke(anotherUser, args);
					} catch (Exception e) {
						throw new InvalidAttributeException();
					}
				}

				this.user = anotherUser;
				userDAO.update(anotherUser);
				return true;
			} else
				;
		}
		return false;
	}

	/**
	 * Add a blog
	 * @param title
	 * @param description
	 * @return blogId
	 * @throws Exception
	 */
	public String addBlog(String title, String description) throws Exception {
		String blogId = lastIdOfBlog();
		Blog newBlog = new Blog(title,description,blogId, this.getLogin());
		blogList.add(newBlog);
		return blogId;
	}

	/**
	 * Delete the user
	 */
	public void delete() {
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();)
			it.next().delete();
		
		userDAO.delete(user);
		
	}

	/**
	 * Converts a string to a valid Date.
	 * 
	 * @param date - String to be converted
	 * @return A Calendar instance if could parte into a Date, null otherwise.
	 */
	private Calendar parseStringToDate(String date) {
		try {
			Calendar cal = new GregorianCalendar();

			String[] arrayStringdate = date.split("/");
			String day = arrayStringdate[0];
			String month = arrayStringdate[1];
			String year = arrayStringdate[2];

			cal.setLenient(false);
			cal.set(GregorianCalendar.YEAR, new Integer(year));
			cal.set(GregorianCalendar.MONTH, new Integer(month) - 1); // Jan = 0, Fev = 1...
			cal.set(GregorianCalendar.DATE, new Integer(day));

			cal.getTime(); // exception thrown here if date is invalid

			return cal;

		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static String getInformationProfile(String login, String attribute) throws Exception  {

		Iterator<UserBean> users = userDAO.list().iterator();

		Map<String, String> pt_to_en = new HashMap<String,String>();

		{
			pt_to_en.put("nome_exibicao", "Name");
			pt_to_en.put("login", "Login");
			pt_to_en.put("email", "Email");
			pt_to_en.put("sexo", "Gender");
			pt_to_en.put("endereco", "Address");
			pt_to_en.put("interesses", "Interests");
			pt_to_en.put("quem_sou_eu", "WhoIAm");
			pt_to_en.put("filmes", "Movies");
			pt_to_en.put("livros", "Books");
			pt_to_en.put("musicas", "Musics");
		}

		try {
			while(users.hasNext()){
				UserBean anotherUser = users.next();							
				if (anotherUser.getLogin().equals(login)){
					if(attribute.equals("dataNasc")){
						String result = "";
						if (anotherUser.getDateOfBirth().get(Calendar.DATE) < 10)
							result += "0";
						result += anotherUser.getDateOfBirth().get(Calendar.DATE) + "/";
						if (anotherUser.getDateOfBirth().get(Calendar.MONTH) < 10)
							result += "0";
						result += (anotherUser.getDateOfBirth().get(Calendar.MONTH) + 1 ) + "/"; // + 1 To avoid missmatch GregorianCalendar calls Jan as 0, Fev as 1...
						result += anotherUser.getDateOfBirth().get(Calendar.YEAR);
						return result;
					} else {
						Class userBeanClass = anotherUser.getClass();
						Method thisMethod = userBeanClass.getMethod("get"+ pt_to_en.get(attribute), (Class[]) null);
						Object result = thisMethod.invoke(anotherUser, (Object[]) null); // we can receive a null
						if (attribute.equals("nome_exibicao") && result == null)
							return login;
						else
							return result.toString();
					}
				} else
					;
			}
			throw new InvalidAttributeException();
		} catch (Exception e) {
			throw new InvalidAttributeException();
		}


	}

	/**
	 * @return This user's login
	 */
	public String getLogin() {
		return user.getLogin();
	}

	/**
	 * Add a post
	 * @param blogId
	 * @param title
	 * @param content
	 * @param date
	 * @return postId
	 * @throws Exception
	 */
	public String addPost(String blogId, String title, String content, String date) throws Exception{
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			
			if(blogId.contains(blog.getId()))
				return blog.getSubBlogObject(blogId).addPost(title, content, date.toString());
		}
		throw new InvalidBlogException();
	}
	
	/**
	 * Checks if the user contains blog passed as parameter
	 * @param id
	 * @return true 
	 * 			- if user contains blog
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasBlog(String blogId){
		return blogId.contains(user.getId());
	}
	
	/**
	 * Checks if the user contains post passed as parameter
	 * @param id
	 * @return true 
	 * 			- if user contains post
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasPost(String postId){
		return postId.contains(user.getId());
	}

	/**
	 * Checks if the user contains sound passed as parameter
	 * @param id
	 * @return true 
	 * 			- if user contains sound
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasSound(String soundId){
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			if(it.next().hasSound(soundId))
				return true;
		}
				
		return false;
	}
	
	/**
	 * Returns post information for attribute
	 * @param postID - id of post
	 * @param attribute
	 * @return post information for attribute
	 * @throws Exception
	 */
	public String getPostInformation(String postId, String attribute) throws Exception{
		Blog blog;
		
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId)){
				return blog.getPostInformation(postId, attribute);
				
			}
			
		}
		
		return null;
	}
	
	/**
	 * Returns the number of sounds from post
	 * @param postId
	 * 			- Identifier of the post sounds to be counted
	 * @return number of sounds
	 * @throws Exception
	 */
	public long getNumberOfSoundsOfPost(String postId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId)){
				return blog.getNumberOfSoundsOfPost(postId);
			}
			
		}
		return 0;
	}
	
	/**
	 * Returns the number of pictures from post
	 * @param postId
	 * 			- Identifier of the post pictures to be counted
	 * @return number of pictures
	 * @throws Exception
	 */
	public long getNumberOfPicturesOfPost(String postId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId)){
				return blog.getNumberOfPicturesOfPost(postId);
			}
		}
		return 0;
	}
	
	/**
	 * Returns the number of movies from post
	 * @param postId
	 * 			- Identifier of the post movies to be counted
	 * @return number of movies
	 * @throws Exception
	 */
	public long getNumberOfMoviesOfPost(String postId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId)){
				return blog.getNumberOfMoviesOfPost(postId);
			}
		}
		return 0;
	}
	
	/**
	 * Capturing the picture of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the picture
	 * @param index
	 * 			- Picture's index
	 * @return pictureId
	 * @throws Exception
	 */
	public String getPicture(String postId, long index) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPicture(postId)){
				return blog.getPicture(postId, index);
			}
		}
		return null;
	}
	
	/**
	 * Capturing the movie of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the movie
	 * @param index
	 * 			- Movie's index
	 * @return movieId
	 * @throws Exception
	 */
	public String getMovie(String postId, long index) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId))
				return blog.getMovie(postId, index);
		}
		return null;
	}
	
	/**
	 * Capturing the sound of the post with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the sound
	 * @param index
	 * 			- Sound's index
	 * @return soundId
	 * @throws Exception
	 */
	public String getSound(String postId, long index) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId)){
				return blog.getSound(postId, index);
			}
		}
		return null;
	}

	
	/**
	 * Checks if the user contains media passed as parameter
	 * @param id
	 * @return true 
	 * 			- if user contains media
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasMedia(String mediaId) {
		return mediaId.contains(user.getId());
	}

	/**
	 * Returns data of media
	 * @param soundId
	 * @return data of media
	 * @throws Exception 
	 */
	public String getMediaData(String mediaId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasMedia(mediaId)){
				return blog.getMediaData(mediaId);
			}
		}
		return null;

	}

	/**
	 * Returns description of media
	 * @param mediaId
	 * @return description of media
	 * @throws Exception 
	 */
	public String getMediaDescription(String mediaId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasMedia(mediaId)){
				return blog.getMediaDescription(mediaId);
			}
		}
		return null;
	}

	
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (!user.equals(other.user))
			return false;
		return true;
	}

	
	/**
	 * Returns the last if of blog
	 * @return last id of blog
	 */
	private String lastIdOfBlog(){
		int last = Integer.parseInt(user.getLastIdOfBlog()) + 1;
		user.setLastIdOfBlog(String.valueOf(last));
		userDAO.update(user);
		return user.getId() + "b" + user.getLastIdOfBlog();
		
	}
	
	//US 7
	
	/**
	 * Returns the number of blogs
	 * @return number of blogs
	 */
	public int getNumberOfBlogs(){
		return blogList.size();
	}
	
	/**
	 * Retrieves the id of blog with te index passed as parameter
	 * @param index
	 * @return id of blog
	 */
	public String getBlogAtIndex(int index){
		if(index < 0 || index > blogList.size())
			return null; //throw new Exception("Index Invalido");
		return blogList.get(index).getId();
	}

	/**
	 * Returns the number of posts blog passed as parameter
	 * @param blogId
	 * @return number of posts
	 */
	public int getNumberOfPostsByBlog(String blogId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.getId().equals(blogId))
				return blog.getNumberOfPosts();
		}
		return 0; //throw new Exception("Blog Invalido");
	}
	
	/**
	 * Capturing the post with the blogId and the index passed as parameter
	 * @param blogId
	 * @param index
	 * 			- Post's index
	 * @return postId
	 */
	public String getPostAtIndex(String blogId, int index) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.getId().equals(blogId))
				return blog.getPostAtIndex(index);
		}
		return null; //throw new Exception("Blog Invalido");
		
	}

	/**
	 * Delete a movie passed as parameter
	 * @param movieId
	 */
	public void deleteMovie(String movieId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasMedia(movieId))
				blog.deleteMovie(movieId);
		}
		
		//throw 
	}

	/**
	 * Delete a sound passed as parameter
	 * @param soundId
	 */
	public void deleteSound(String soundId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasMedia(soundId))
				blog.deleteSound(soundId);
		}		
	}

	/**
	 * Delete a picture passed as parameter
	 * @param pictureId
	 */
	public void deletePicture(String pictureId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasMedia(pictureId))
				blog.deletePicture(pictureId);
		}
		
	}
	/**
	 * 
	 * Add a comment
	 * @param postId
	 * 			- identifier of the post which is inserted into the comment
	 * @param comment
	 * 			- Text's comment
	 * @return
	 * @throws Exception
	 */
	public String addComment(String postId, String comment, String login) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId))
				return blog.addComment(postId, comment, login);
		}
		return null;
	}

	/**
	 * Returns the number of comments of post
	 * @param postId
	 * 			- Identifier of the post which will capture the number of comments
	 * @return number of posts
	 * @throws Exception
	 */
	public long getNumberOfComments(String postId) {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId))
				return blog.getNumberOfComments(postId);
		}
		return 0;
	}

	/**
	 * Capturing the comment with the index passed as parameter
	 * @param postId
	 * 			- Identifier of the post which will capture the comment
	 * @param index
	 * 			- comment's index
	 * @return commentId
	 */
	public String getComment(String postId, int index){
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId))
				return blog.getComment(postId, index);
		}

		return null;
	}

	/**
	 * Checks if the post contains comment passed as parameter
	 * @param id
	 * @return true 
	 * 			- if post contains comment
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasComment(String commentId) {

		return commentId.contains(user.getId());
		//		Blog blog;
//		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
//			blog = it.next();
//			if(blog.hasCommet(commentId))
//				return true;
//		}
//		return false;
	}

	/**
	 * Retrieves the comment's text passed as parameter
	 * @param commentId
	 * 			- identifier of the comment that the text will be recovered
	 * @return comment's text
	 * @throws Exception
	 */
	public String getCommentText(String commentId) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasComment(commentId))
				return blog.getCommentText(commentId);
		}
		return null;
	}

	/**
	 * Retrieves the comment's author passed as parameter
	 * @param commentId
	 * 			- identifier of the comment that the author will be recovered
	 * @return comment's author
	 * @throws Exception
	 */
	public String getCommentAuthor(String commentId) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasComment(commentId))
				return blog.getCommentAuthor(commentId);
		}
		return null;
	}

	/**
	 * Delete a blog passed as parameter
	 * @param blogId
	 * @throws Exception
	 */
	public void deleteBlog(String blogId) throws Exception {
		Blog blog, theBlog;
		theBlog = null;
		
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.getId().equals(blogId)){
				theBlog = blog;
				break;
			}
		}
		if(theBlog == null)
			throw new InvalidBlogException();
		blogList.remove(theBlog);
		theBlog.delete();

	}

	/**
	 * Modifies the information of the post passed as parameter
	 * @param postId
	 * 			- Identifier of the post modification
	 * @param attribute
	 * 			- Attribute to be modified
	 * @param value
	 * 			- Value that will be inserted
	 * @throws Exception
	 */
	public void changePostInformation(String postId, String attribute,
			String value) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blog.hasPost(postId))
				blog.changePostInformation(postId, attribute, value);
		}
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Retrieves the password of the user
	 * @return user's password
	 */
	public String getPassword(){
		return user.getPassword();
	}
	
	/**
	 * Modifies the information of the blog passed as parameter
	 * @param blogId
	 * 			- blogId will be modifies
	 * @param attribute
	 * 			- Attribute will be modifies
	 * @param newValue
	 * 			- NewValue will be inserted
	 * @return true 
	 * 			- if the update was completed successfully
	 * 		   false
	 * 			- otherwise 
	 * @throws Exception
	 */
	public void updateBlog(String blogId, String attribute, String newValue) throws Exception {
		Blog blog;
		for (Iterator<Blog> it = blogList.iterator(); it.hasNext();) {
			blog = it.next();
			if(blog.getId().equals(blogId)){
				blog.updateBlog(attribute, newValue);
				return;
			}
		}
		throw new InvalidBlogException();
		
	}

	/**
	 * Delete a post passed as parameter
	 * @param postId
	 * @throws Exception
	 */
	public void deletePost(String postId) throws Exception {
		Blog blog;
		for (Iterator<Blog> it = blogList.iterator(); it.hasNext();) {
			blog = it.next();
			if(blog.hasPost(postId)){
				blog.deletePost(postId);
				
			}
		}
	}

	/**
	 * Returns a list of blog by name 
	 * @param match
	 * @return list of blog
	 * @throws Exception
	 */
	public List<String> findBlogByName(String match) throws Exception {
		List<String> blogs = new ArrayList<String>();
		Blog blog;
		for (Iterator<Blog> it = blogList.iterator(); it.hasNext();) {
			blog = it.next();
			if(Blog.getInformationBlog(blog.getId(), "titulo").contains(match))
				blogs.add(blog.getId());	
		}
		return blogs;
	}

	/**
	 * Retrieves the number of comments of login passed as parameter
	 * @param login
	 * @return number of comments
	 */
	public int getNumberOfCommentsByLogin(String login) {
		int numberOfComments = 0;
		for (Iterator<Blog> it = blogList.iterator(); it.hasNext();) {
			numberOfComments += it.next().getNumberOfCommentsByLogin(login);
		}
		return numberOfComments;
		
		
	}

	/**
	 * Returns a number of announcements
	 * @return number of announcements
	 */
	public int getNumberOfAnnouncements() {
		return announcementList.size();
	}

	/**
	 * Add a post announcement
	 * @param blogId
	 */
	public void addPostAnnouncements(String blogId) {
		announcementList.add(new Announcement( lastAnnouncementId(), blogId));
		
	}

	/**
	 * Delete a announcement
	 * @param announcementId
	 */
	public void deleteAnnouncement(String announcementId) {
		Announcement a, a2;
		a2 = null;
		for (Iterator<Announcement> it = announcementList.iterator(); it.hasNext();) {
			a = it.next();
			if(announcementId.equals(a.getId()));
				a2 = a;
		}

		if(a2!= null){
			announcementList.remove(a2);
			a2.delete();
		}


		
	}

	/**
	 * Returns a announcement of index passed as parameter
	 * @param index
	 * @return id of announcement 
	 */
	public String getAnnouncement(int index) {
		return announcementList.get(index).getId();
	}
	
	//TODO Implement that
	/**
	 * Returns the last announcementId
	 * @return last announcementId
	 */
	private String lastAnnouncementId(){
		return user.getId() + "a";
	}


	/**
	 * Checks if the user contains announcement passed as parameter
	 * @param announcementId
	 * @return true 
	 * 			- if user contains announcement
	 * 		   false
	 * 			- otherwise
	 */
	public boolean hasAnnouncement(String announcementId) {
		return announcementId.contains(user.getId());
	}

	/**
	 * Returns a announcement blogId
	 * @param announcementId
	 * @return 
	 */
	public String getAnnouncementBlogId(String announcementId) {
		Announcement a;
		for (Iterator<Announcement> it = announcementList.iterator(); it.hasNext();) {
			a = it.next();
			if(a.getId().equals(announcementId))
				return a.getBlogId();
		}
		return null;
	}

	//US14
	/**
	 * Create a sub blog
	 * @param blogId
	 * @param title
	 * @param description
	 * @return subBlogId
	 * @throws Exception
	 */
	public String createSubBlog(String blogId, String title, String description) throws Exception{
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blogId.contains(blog.getId()) || blogId.equals(blog.getId())){
				return blog.addSubBlog(blogId, title, description);
			}
		}
		throw new InvalidBlogException();
	}

	/**
	 * Returns a number of sub blogs of blog passed as parameter
	 * @param blogId
	 * @return number of sub blogs
	 * @throws Exception
	 */
	public int getNumberOfSubBlogs(String blogId) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blogId.contains(blog.getId()))
				return blog.getNumberOfSubBlogs(blogId);
		}
		throw new InvalidBlogException();
	}
	
	/**
	 * Returns a number of all sub blogs of blog passed as parameter
	 * @param blogId
	 * @return number of all sub blogs
	 * @throws Exception
	 */
	public int getNumberOfAllSubBlogs(String blogId) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blogId.contains(blog.getId()))
				return blog.getNumberOfAllSubBlogs(blogId);
		}
		throw new InvalidBlogException();
	}

	/**
	 * Returns id of sub blog of index as passed as parameter
	 * @param blogId
	 * @param index
	 * @return subBlogId
	 * @throws Exception
	 */
	public String getSubBlog(String blogId, int index) throws Exception{
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blogId.contains(blog.getId()))
				return blog.getSubBlog(blogId, index);
		}
		throw new InvalidBlogException();
		
	}

	/**
	 * Returns a number of all posts of blog passed as parameter
	 * @param blogId
	 * @return number of all posts
	 * @throws Exception
	 */
	public int getNumberOfAllPosts(String blogId) throws Exception {
		Blog blog;
		for(Iterator<Blog> it = blogList.iterator(); it.hasNext();){
			blog = it.next();
			if(blogId.contains(blog.getId()))
				return blog.getNumberOfAllPosts(blogId);
		}
		throw new InvalidBlogException();
	}
}


